package com.bw.lyc.mvp.model.entity;

import com.bw.lyc.mvp.base.model.BaseEntity;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class NewsEntity extends BaseEntity {

    public final static int NONECODE = 0;
    public final static int IMG1CODE = 1;
    public final static int IMG2CODE = 2;
    public final static int IMG3CODE = 3;

    public String values;

    public List<Values>getValues(){
        if (values==null)
            return null;
        if (values.isEmpty())
            return null;
        List<Values> list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(values);
            for (int i = 0;i < jsonArray.length();i++){
                String str = jsonArray.getString(i);
                Values entity = new Gson().fromJson(str,Values.class);
                list.add(entity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static class Values implements MultiItemEntity {
        public int news_type;
        public String news_author;
        public String news_value;
        public String news_title;
        public String news_url;
        public long news_time;
        public long author_id;
        public long channel_id;
        public long news_id;
        public List<MediaValues> medias;

        public List<MediaValues>getMedias(){
            return medias;
        }

        @Override
        public int getItemType() {
            if (getMedias()==null)
                return NONECODE;
            if (getMedias().size()==0)
                return NONECODE;
            if (getMedias().size() == 1)
                return IMG1CODE;
            if (getMedias().size() == 2)
                return IMG2CODE;
            if (getMedias().size() == 3)
                return IMG3CODE;
            return NONECODE;
        }
    }
    public static class MediaValues{
        public int media_type;
        public long media_id;
        public String media_url;
    }

}